<?php
/**
 * Cms_Inventory
 *
 * @category   Cms
 * @package    Cms_Model
 */
class Cms_Model_Inventory extends BIS_Model_Abstract
{

     /**
     * Get Inventory by its entryId
     *
     * @param  int $id The id
     * @return Cms_Resource_Inventory_Item|null
     */
    public function getInventoryByEntryId($id)
    {
        $id = (int) $id;

        return $this->getResource('Inventory')->getInventoryByEntryId($id);
    }

    /**
     * Get list of Inventories
     *
     * @param null|array  $data  sort, order, page, limit, search
     * @return Zend_Db_Table_Rowset|Zend_Paginator
     */
    public function getInventories($data = null)
    {
    	if ($data) {
    
    		$sortData = array('product', 'supplier', 'quantity', 'price');
    
    		$order = '';
    		if (isset($data['sort']) && in_array($data['sort'], $sortData)) {
    			$order .= $data['sort'];
    		} else {
    			$order .= "product";
    		}
    
    		if (isset($data['order']) && ($data['order'] == 'desc')) {
    			$order .= " DESC";
    		} else {
    			$order .= " ASC";
    		}
    
    		$data['order'] = $order;
    
    		if (isset($data['page'])) {
    			if ($data['page'] < 0) {
    				$data['page'] = 0;
    			}
    		}
    
    		if (isset($data['limit'])) {
    			if ($data['limit'] < 1) {
    				$data['limit'] = 20;
    			}
    		}else{
    			$data['limit'] = 20;
    		}
    
    		return $this->getResource('Inventory')->getInventories($data);
    
    	} else {
    		return $this->getResource('Inventory')->getInventories();
    	}
    }
    
    /**
     * Get Total Inventory By Product Id
     *
     * @param int      $productId Product Id
     * @return int     Total inventory for the product
     */
    public function getTotalInventoryByProductId($productId)
    {
        $productId = (int) $productId;

        return $this->getResource('Inventory')->getTotalInventoryByProductId($productId);
    }

    /**
     * Get Total Inventory By Supplier Id
     *
     * @param int      $supplierId Supplier Id
     * @return int     Total inventory for the Supplier
     */
    public function getTotalInventoryBySupplierId($supplierId)
    {
        $supplierId = (int) $supplierId;

        return $this->getResource('Inventory')->getTotalInventoryBySupplierId($supplierId);
    }
  
    /**
     * Save Inventory
     *
     * @param array  $data The data to insert/update
     * @param string $savedBy The full name of the administrator that saved the data
	 * @param Zend_DB_Table_Row $row Optional The row to use
     * @return mixed The primary key
     */
    public function saveInventory($data, $savedBy, $row = null)
    {
    	$currentTime = date('Y-m-d H:i:s');

        // convert orderDate to mysql data type
        if (array_key_exists('orderDate', $data) && '' != $data['orderDate']) {
            $mysqlDate = new Zend_Date($data['orderDate'], 'dd.MM.yyyy');
            $data['orderDate'] = $mysqlDate->toString('yyyy-MM-dd');
        }
        
    	if(null !== $row){
			$data['lastUpdatedBy'] = $savedBy;
			$data['lastUpdatedDate'] = $currentTime;
		}else{
			$data['addedBy'] = $savedBy;
			$data['addedDate'] = $currentTime;
		}

        return $this->getResource('Inventory')->saveRow($data, $row);
    }


    /**
     * Delete Inventory
     *
     * @param Zend_DB_Table_Row $row The row to use
     * @return int The number of rows deleted
     */
    public function deleteInventory($row)
    {
        return $this->getResource('Inventory')->deleteRow($row);
    }

    /**
     *  Get inventory report
     *
     * @param int|null   $productId     Product id
     * @param string|null   $fromDate	From date added
	 * @param string|null   $toDate     To date added
     * @return Zend_Db_Table_Rowset
     */
    public function getInventoryReport($productId=null, $fromDate=null, $toDate=null){

        return $this->getResource('Inventory')->getInventoryReport($productId, $fromDate, $toDate);

    }
    
    
    /**
     * Get Credit Purchase by its entryId
     *
     * @param  int $id The id
     * @return Cms_Resource_CreditPurchase_Item|null
     */
    public function getCreditPurchaseByEntryId($id)
    {
    	$id = (int) $id;
    
    	return $this->getResource('CreditPurchase')->getCreditPurchaseByEntryId($id);
    }
    
    /**
     * Get list of credit purchases
     *
     * @param null|array  $data  sort, order, page, limit, search
     * @return Zend_Db_Table_Rowset|Zend_Paginator
     */
    public function getCreditPurchases($data = null)
    {
    	if ($data) {
    
    		$sortData = array('product', 'supplier', 'quantity', 'price');
    
    		$order = '';
    		if (isset($data['sort']) && in_array($data['sort'], $sortData)) {
    			$order .= $data['sort'];
    		} else {
    			$order .= "product";
    		}
    
    		if (isset($data['order']) && ($data['order'] == 'desc')) {
    			$order .= " DESC";
    		} else {
    			$order .= " ASC";
    		}
    
    		$data['order'] = $order;
    
    		if (isset($data['page'])) {
    			if ($data['page'] < 0) {
    				$data['page'] = 0;
    			}
    		}
    
    		if (isset($data['limit'])) {
    			if ($data['limit'] < 1) {
    				$data['limit'] = 20;
    			}
    		}else{
    			$data['limit'] = 20;
    		}
    
    		return $this->getResource('CreditPurchase')->getCreditPurchases($data);
    
    	} else {
    		return $this->getResource('CreditPurchase')->getCreditPurchases();
    	}
    }
    
    /**
     * Get Total Credit Purchase By Product Id
     *
     * @param int      $productId Product Id
     * @return int     Total credit purchase for the product
     */
    public function getTotalCreditPurchaseByProductId($productId)
    {
    	$productId = (int) $productId;
    
    	return $this->getResource('CreditPurchase')->getTotalCreditPurchaseByProductId($productId);
    }
    
    /**
     * Get Total Credit Purchase By Supplier Id
     *
     * @param int      $supplierId Supplier Id
     * @return int     Total credit purchase for the Supplier
     */
    public function getTotalCreditPurchaseBySupplierId($supplierId)
    {
    	$supplierId = (int) $supplierId;
    
    	return $this->getResource('CreditPurchase')->getTotalCreditPurchaseBySupplierId($supplierId);
    }
    
    /**
     * Save Credit Purchase
     *
     * @param array  $data The data to insert/update
     * @param string $savedBy The full name of the administrator that saved the data
     * @param Zend_DB_Table_Row $row Optional The row to use
     * @return mixed The primary key
     */
    public function saveCreditPurchase($data, $savedBy, $row = null)
    {
    	$currentTime = date('Y-m-d H:i:s');
    
    	// convert orderDate to mysql data type
    	if (array_key_exists('orderDate', $data) && '' != $data['orderDate']) {
    		$mysqlDate = new Zend_Date($data['orderDate'], 'dd.MM.yyyy');
    		$data['orderDate'] = $mysqlDate->toString('yyyy-MM-dd');
    	}
    
    	if(null !== $row){
    		$data['lastUpdatedBy'] = $savedBy;
    		$data['lastUpdatedDate'] = $currentTime;
    	}else{
    		$data['addedBy'] = $savedBy;
    		$data['addedDate'] = $currentTime;
    	}
    
    	return $this->getResource('CreditPurchase')->saveRow($data, $row);
    }
    
    
    /**
     * Delete Credit Purchase
     *
     * @param Zend_DB_Table_Row $row The row to use
     * @return int The number of rows deleted
     */
    public function deleteCreditPurchase($row)
    {
    	return $this->getResource('CreditPurchase')->deleteRow($row);
    }

}